
function Teavar = VAROLS(yy,q,uni)

xdata = [];
nx=0;
[T,K]           = size(yy);

R = eye(K);

y = yy(q+1:end,:);

X=zeros(T-q,K,q);
for ix=1:q
    X(:,:,ix)=yy(q+1-ix:end-ix,:);
end

X=[xdata X(:,:)];


%% Calculate D
D = eye(K);


%% Calculate T (See Canova, p.354)

V_post      = eye(size(kron(R*D*R',X'*X)))/(kron(R*D*R',X'*X));
T_post      = V_post*( kron(R*D*R',X)'*vec(y));

Tnorm       = T_post ;%mvnrnd1(T_post, V_post,1);

Tnorm             = Tnorm';
Tb                 =reshape(Tnorm,[K*q + nx,K]);
Tbb                =Tb(1:K*q,:);
Tbbb               =reshape(Tbb,K,q,K); % variables, q, equations
Tbbb               = permute(Tbbb,[3,1,2]);% equations, variables, q

Tea            = zeros(K,K,q);
if uni==1
    Tea(1:K,1:K,1) = diag(diag(Tbbb(1:K,1:K,1)));
    if q>1
        for qx=2:q
        Tea(1:K,1:K,qx) =  diag(diag(Tbbb(1:K,1:K,qx)));
        end
    end

end


Teavar = Tea(:,:);


% 
